package ru.rchervot.sanasar.client.view;

import ru.rchervot.sanasar.client.model.FactoryQueryCandidates;
import ru.rchervot.sanasar.client.rpc.RPCServiceAsync;
import ru.rchervot.sanasar.client.view.Presenter.Display;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class FactorySearchResultsViewCandidates implements FactorySearchResultsView {
		
		private final HandlerManager eventBus;
		private final RPCServiceAsync service;
		private final FactoryQueryCandidates factoryQuery;
		
		@Inject
		public FactorySearchResultsViewCandidates(
				HandlerManager eventBus, RPCServiceAsync service,
				FactoryQueryCandidates factoryQuery) {
			this.eventBus = eventBus;
			this.service = service;
			this.factoryQuery = factoryQuery;
		}
		
		public Display build(int pageSize) {
			return (Display)getContentResultsView(pageSize);
		}
	
		private ContentSearchResultsView contentResultsView;
		private Widget getContentResultsView(int pageSize) {
			 if (contentResultsView == null) {
				 contentResultsView = new ContentSearchResultsView(
							getResultsHeaderView(), getResultsView(), 
							getResultsFooterView(), getRecordHeaderView(), 
							getRecordView(), getRecordFooterView(),
							getSelectedNone(), getSelectedSeveral());
					
				 ContentSearchResultsPresenter p = 
					 new ContentSearchResultsPresenter(contentResultsView,
						 eventBus, service, factoryQuery);
				 p.bindDisplay();
			 }
			 
			 return contentResultsView;
		}
		
		private ResultsHeaderCandidatesView resultsHeaderView;
		Widget getResultsHeaderView() {
			if (resultsHeaderView == null) {
				resultsHeaderView = new ResultsHeaderCandidatesView();
				ResultsHeaderCandidatesPresenter p = new ResultsHeaderCandidatesPresenter(
						resultsHeaderView, eventBus);
				p.bindDisplay();
			}
			
			return resultsHeaderView;
		}
		
		private ResultsCandidatesView resultsView;
		private Widget getResultsView() {
			if (resultsView == null) {
				resultsView = new ResultsCandidatesView();
				ResultsCandidatesPresenter results = new ResultsCandidatesPresenter(
						resultsView, eventBus, service);
				results.bindDisplay();
			}
			
			return resultsView;
		}
		
		private ResultsFooterCandidatesView resultsFooterView;
		Widget getResultsFooterView() {
			if (resultsFooterView == null) {
				resultsFooterView = new ResultsFooterCandidatesView();
				ResultsFooterCandidatesPresenter p = new ResultsFooterCandidatesPresenter(
						resultsFooterView, eventBus, service);
				p.bindDisplay();
			}
			
			return resultsFooterView;
		}
		
		private RecordHeaderCandidatesView recordHeaderView;
		private Widget getRecordHeaderView() {
			if (recordHeaderView == null) {
				recordHeaderView = new RecordHeaderCandidatesView();
				RecordHeaderCandidatesPresenter p = new RecordHeaderCandidatesPresenter(
						recordHeaderView, eventBus);
				p.bindDisplay();
			}
			
			return recordHeaderView;
		}
		
		private RecordCandidatesView recordView;
		private Widget getRecordView() {
			if (recordView == null) {
				recordView = new RecordCandidatesView((Widget)getRecordDescription());
				RecordCandidatesPresenter p = new RecordCandidatesPresenter(
						recordView, eventBus, service);
				p.bindDisplay();
			}
			
			return recordView;
		}
		
		private RecordSummaryCandidatesView recordDescription;
		private Widget getRecordDescription() {
			if (recordDescription == null) {
				recordDescription = new RecordSummaryCandidatesView();
				Presenter p = new RecordSummaryCandidatesPresenter(
						recordDescription, eventBus);
				p.bindDisplay();
			}
			
			return recordDescription;
		}
		
		private RecordFooterCandidatesView recordFooterView;
		private Widget getRecordFooterView() {
			if (recordFooterView == null) {
				recordFooterView = new RecordFooterCandidatesView();
				RecordFooterCandidatesPresenter p = 
					new RecordFooterCandidatesPresenter(recordFooterView,
							eventBus, service);
				p.bindDisplay();
			}
			
			return recordFooterView;
		}
		
		private SelectedNoneView selectedNone;
		private Widget getSelectedNone() {
			if (selectedNone == null) {
				selectedNone = new SelectedNoneView();
				SelectedNonePresenter p = new SelectedNonePresenter(selectedNone, 
						eventBus);
				p.bindDisplay();
			}
			
			return selectedNone;
		}
		
		private SelectedSeveralView selectedSeveral;
		private Widget getSelectedSeveral() {
			if (selectedSeveral == null) {
				selectedSeveral = new SelectedSeveralView();
				SelectedSeveralPresenter p = new SelectedSeveralPresenter(
						selectedSeveral, eventBus);
				p.bindDisplay();
			}
			
			return selectedSeveral;
		}
}
